//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.UIDesigner
{
    #region NameSpace
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Research.ScientificWorkflow.TridentModel;
    using System.Windows.Media;
    using System.Windows.Shapes;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Controls;
    using Microsoft.Research.ScientificWorkflow.TridentUtilities;
    using System.Windows.Data;

    #endregion

    //Need to add
    public class ActivityInputParameterElement : ActivityParameterDesigner
    {
        /// <summary>
        /// The composer control holding this element.
        /// </summary>
        private ComposerControl composerCtrl;

        #region Constructor

        public ActivityInputParameterElement(ParameterDescriptionModel parameter, TridentUIActivityElement parent, BaseModel model, Dictionary<string, DataTypeModel> dataTypes)
            : base(parameter, parent, ParameterFlowType.InputParameter, model)
        {
            this.Loaded += new RoutedEventHandler(this.OnLoaded);
            this.CreateInputParameter(parameter, dataTypes);
        }

        #endregion

        #region Private Method

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            this.GetComposerControl();
            this.ContextMenu = this.CreateContextMenu(this.ParamModel);
        }


        /// <summary>
        /// Creates Input parameter 
        /// </summary>
        /// <param name="param">ParameterDescriptionModel</param>
        private void CreateInputParameter(ParameterDescriptionModel param, Dictionary<string, DataTypeModel> dataTypes)
        {
            this.ParamFillBrush = ActivityInputParameterElement.CreateLightParamBrush(param, dataTypes);
            this.AddParameterShape();
            this.AddParameterElementTextBlock(param.Label);
        }

        /// <summary>
        /// Get the composer control instance.
        /// </summary>
        private void GetComposerControl()
        {
            string composerCtrlName = (string)this.GetValue(ComposerControl.ComposerControlNameProperty);
            this.composerCtrl = Application.Current.MainWindow.FindName(composerCtrlName) as ComposerControl;
        }

        private ContextMenu CreateContextMenu(ParameterDescriptionModel param)
        {

            if (this.composerCtrl != null && this.composerCtrl.CurrentRenderMode == RenderMode.ComposerMode)
            {
                MenuItem makeMandatory = new MenuItem();
                makeMandatory.Header = TridentResourceManager.GetString("MakeMandatoryForWorkflowContextMenu");
                makeMandatory.Name = "makeMandatory";
                Binding mandatoryVisibilityBinding = new Binding("IsNotMandatoryForWorkflow");
                mandatoryVisibilityBinding.Mode = BindingMode.OneWay;
                mandatoryVisibilityBinding.Source = param;
                mandatoryVisibilityBinding.UpdateSourceTrigger = UpdateSourceTrigger.Default;
                mandatoryVisibilityBinding.Converter = new BooleanToVisibilityConverter();
                makeMandatory.SetBinding(MenuItem.VisibilityProperty, mandatoryVisibilityBinding);
                makeMandatory.Click += new RoutedEventHandler(this.OnMakeMandatoryClick);
                makeMandatory.Icon = TridentUIActivityElement.GetIcon(@"pack://Application:,,,/Microsoft.Research.ScientificWorkflow.TridentUtilities;component/Images/ScientificWorkflow/mandatory.png");

                MenuItem makeOptional = new MenuItem();
                makeOptional.Header = TridentResourceManager.GetString("MakeOptionalForWorkflowContextMenu");
                makeOptional.Name = "makeOptional";
                Binding optionalVisibilityBinding = new Binding("IsMandatoryForWorkflow");
                optionalVisibilityBinding.Mode = BindingMode.OneWay;
                optionalVisibilityBinding.Source = param;
                optionalVisibilityBinding.UpdateSourceTrigger = UpdateSourceTrigger.Default;
                optionalVisibilityBinding.Converter = new BooleanToVisibilityConverter();
                makeOptional.SetBinding(MenuItem.VisibilityProperty, optionalVisibilityBinding);
                makeOptional.Click += new RoutedEventHandler(this.OnMakeOptionalClick);
                makeOptional.Icon = TridentUIActivityElement.GetIcon(@"pack://Application:,,,/Microsoft.Research.ScientificWorkflow.TridentUtilities;component/Images/ScientificWorkflow/optional.png");

                this.ContextMenu = new ContextMenu();
                this.ContextMenu.Items.Add(makeMandatory);
                this.ContextMenu.Items.Add(makeOptional);
            }

            return this.ContextMenu;
        }

        private void OnMakeMandatoryClick(object sender, RoutedEventArgs e)
        {
            if (this.ParamModel.IsDatabound)
            {
                TridentMessageBox.ShowTridentErrorMessageBox(
                               Application.Current.MainWindow,
                               TridentResourceManager.GetString("ParamElementActivityBoundError"));
                return;
            }

            this.ParamModel.IsMandatoryForWorkflow = true;
            this.AssignParameterBrush();
            if (this.composerCtrl != null)
            {
                this.composerCtrl.IsWFCommitted = false;
                this.composerCtrl.CurrentComposerRenderHelper.UpdateInputParametersInPropertyPane(this.Model, this.ParamModel, true);
            }
        }

        private void OnMakeOptionalClick(object sender, RoutedEventArgs e)
        {
            this.ParamModel.IsMandatoryForWorkflow = false;
            this.AssignParameterBrush();
            if (this.composerCtrl != null)
            {
                this.composerCtrl.IsWFCommitted = false;
                this.composerCtrl.CurrentComposerRenderHelper.UpdateInputParametersInPropertyPane(this.Model, this.ParamModel, false);
            }
        }

        /// <summary>
        /// Get the datatype color
        /// </summary>
        /// <param name="param">ParameterDescriptionModel</param>
        /// <returns>SolidColorBrush</returns>
        private static SolidColorBrush CreateLightParamBrush(ParameterDescriptionModel param, Dictionary<string, DataTypeModel> dataTypes)
        {
            // Check whether the Datatype is contained in the Datatypes[]
            Color dataypeColor;
            if (dataTypes.ContainsKey(param.DataType.ToString()))
            {
                DataTypeModel typeModel = dataTypes[param.DataType.ToString()];
                dataypeColor = typeModel.LightEditorColor;
            }
            else
            {
                // if it is not present then Default it to Purple.
                dataypeColor = Colors.Purple;
            }

            return new SolidColorBrush(dataypeColor);
        }

        #endregion
    }
}
